当前位置: 数据库 > MongoDB > MongoDB 条件操作符和demo

MongoDB 条件操作符和demo

2024-05-24 分类:MongoDB 作者:admin 阅读(21)

描述

条件操作符用于比较两个表达式并从 mongoDB 集合中获取数据。

在本章节中,我们将讨论如何在 MongoDB 中使用条件操作符。

这些操作符可以分为以下几类:比较操作符、逻辑操作符、元素操作符、数组操作符、以及其他常用操作符。

比较操作符

比较操作符有:

操作符 描述 示例
$eq 等于 { age: { $eq: 25 } }
$ne 不等于 { age: { $ne: 25 } }
$gt 大于 { age: { $gt: 25 } }
$gte 大于等于 { age: { $gte: 25 } }
$lt 小于 { age: { $lt: 25 } }
$lte 小于等于 { age: { $lte: 25 } }
$in 在指定的数组中 { age: { $in: [25, 30, 35] } }
$nin 不在指定的数组中 { age: { $nin: [25, 30, 35] } }

查找年龄大于 25 且城市为 "New York" 的文档:

逻辑操作符

逻辑操作符有:

操作符 描述 示例
$and 逻辑与,符合所有条件 { $and: [ { age: { $gt: 25 } }, { city: "New York" } ] }
$or 逻辑或,符合任意条件 { $or: [ { age: { $lt: 25 } }, { city: "New York" } ] }
$not 取反,不符合条件 { age: { $not: { $gt: 25 } } }
$nor 逻辑与非,均不符合条件 { $nor: [ { age: { $gt: 25 } }, { city: "New York" } ] }

查找年龄大于 25 或城市为 "New York" 的文档:

元素操作符

元素操作符有:

操作符 描述 示例
$exists 字段是否存在 { age: { $exists: true } }
$type 字段的 BSON 类型 { age: { $type: "int" } }

查找包含 age 字段的文档:

数组操作符

数组操作符有:

操作符 描述 示例
$all 数组包含所有指定的元素 { tags: { $all: ["red", "blue"] } }
$elemMatch 数组中的元素匹配指定条件 { results: { $elemMatch: { score: { $gt: 80, $lt: 85 } } } }
$size 数组的长度等于指定值 { tags: { $size: 3 } }

查找数组 tags 中包含 "red" 和 "blue" 的文档:

其他操作符

还有一些其他操作符如下:

操作符 描述 示例
$regex 匹配正则表达式 { name: { $regex: /^A/ } }
$text 进行文本搜索 { $text: { $search: "coffee" } }
$where 使用 JavaScript 表达式进行条件过滤 { $where: "this.age > 25" }

查找名字以 "A" 开头的文档:

实例

查找年龄大于 25 且城市为 "New York",名字以 "A" 开头的文档:

实例

db.myCollection.find({
$and: [
{ age: { $gt: 25 } },
{ city: "New York" },
{ name: { $regex: /^A/ } }
]
});

执行语句:

查找_id

db.getCollection('ldy_cve_v5').find({ _id: ObjectId("65dea60ad8bcde3b94d82351") })
某个字段不为空
db.getCollection('ldy_cve_v5').find({"containers.cna.cnnvdLevel": {"$exists": true, "$ne": null}})
删除某条
db.getCollection('ldy_cve_v5').deleteMany({
  $or: [
    { 'cveMetadata.cveId': { $exists: false } },
    { 'cveMetadata.cveId': { $eq: "" } }
  ]
})
# 创建唯一索引:
db.ldy_cve_v5_cwe.createIndex( { "cweId":"" }, { unique: true } )
db.getCollection('ldy_cve_v5').find({
  $or: [
    { 'ldyMetadata.ldyId': 'LDY-2023-00186623' },
  ]
})
# 查询情报
db.getCollection('ldy_cve_v5').find({
  $and: [
    { 'qingBao.isPush': 1 },
    { 'ldyLDMetadata': { $not: { $exists: true } } }
  ]
})
db.getCollection('ldy_cve_v5').find({
  $and: [
    { 'containers.cna.isQbAsync': 1 },
    { 'qingBao.isPush': { $exists: false }},
  ]
})
# 设置值为0
db.getCollection('ldy_cve_v5').updateMany(
  {
    $and: [
      { 'containers.cna.isQbAsync': 1 },
      { 'qingBao.isPush': { $exists: false } }
    ]
  },
  { $set: { 'qingBao.isPush': 0 } }
)
// 删除某个字段
db.getCollection('ldy_cve_v5').updateMany({}, {$unset: {qingBao: ""}})
db.getCollection('ldy_cve_v5').find({
  $or: [
    { 'ldyMetadata.ldyId': { $regex: "LDY-2023-00186623", $options: "i" } },
  ]
})
db.getCollection('ldy_cve_v5').find({
  $or: [
    { 'cveMetadata.cveId': 'CVE-2023-0423' },
  ]
})
db.getCollection('ldy_cve_v5').find({
  'cveMetadata.cveId': { $in: ['CVE-2023-0423', 'CVE-2023-0424'] }
})
db.getCollection('ldy_cve_v5_capec').find({
  descriptions: {
    $elemMatch: {
      lang: "cn",
      description: { $regex: "script", $options: "i" }
    }
  }
})
# 某字段存在,不为空
db.getCollection('ldy_cve_v5').find({ "srcMetadata.srcId": { $exists: true, $ne: null } })
# 某字段不存在或者为空
db.getCollection('ldy_cve_v5').find({
  $or: [
    { "isOpenTime": { $exists: false } },   // 查询字段不存在的文档
    { "isOpenTime": { $eq: "" } }          // 查询字段存在且为空的文档
  ]
})
# 查询数组不为空
db.getCollection('ldy_cve_v5').find({ "containers.cna.tags360": { $exists: true, $not: {$size: 0} } }).size()
# 删除所有标签
db.getCollection('ldy_cve_v5').updateMany(
   { },
   { "$set": { "containers.cna.tags360": [] } }
)
db.getCollection('ldy_cve_v5').updateMany(
   { },
   { "$set": { "containers.cna.exps": [] } }
)
db.getCollection('ldy_cve_v5').updateMany(
   { },
   { "$set": { "containers.cna.patchs": [] } }
)
# 查询数据包含某个元素
db.getCollection('ldy_cve_v5').find({ "containers.cna.metrics": { $elemMatch: { "cvssV2_0": {$exists: true} } } })
# 正则
db.getCollection('ldy_cve_v5').find({
    $and: [
        {"cveMetadata.datePublished": {$regex: "^2023"}},
        {
            $or: [
                {"containers.cna.metrics.cvssV3_1": {$exists: true}},
                {"containers.cna.metrics.cvssV3_0": {$exists: true}},
                {"containers.cna.metrics.cvssV2_0": {$exists: true}}
            ]
        }
    ]
})
db.getCollection('ldy_cve_v5').find({
  $and: [
    {
      "cveMetadata.datePublished": {
        $regex: "^2023"
      }
    },
    {
      $or: [
        {
          "containers.cna.metrics.cvssV3_1": {
            $exists: true
          }
        },
        {
          "containers.cna.metrics.cvssV3_0": {
            $exists: true
          }
        },
        {
          "containers.cna.metrics.cvssV2_0": {
            $exists: true
          }
        }
      ]
    },
    {
      "containers.cna.tags360": {
        $not: {
          $elemMatch: {
            "tagId": {
              $in: [
                "CpCS8X/55Ts=",
                "Uv7xjKkYkkE=",
                "5YZzU4ZPhiY="
              ]
            }
          }
        }
      }
    }
  ]
})
# 这个查询会找到所有具有非空字段的文档。 $exists: true 确保字段存在,并且 $ne: [] 则确保该字段不是空数组。这样可以过滤出数组字段不为空的文档。
db.getCollection('ldy_cve_v5').find({
$and:[
{"containers.cna.effects": { $exists: true, $ne: [] } },
{"bugtraqMetadata":{$exists: true}}
]
})

在golang代码中,使用bson查询:

select 组装方式:


「三年博客,如果觉得我的文章对您有用,请帮助本站成长」

赞(0) 打赏

支付宝
微信
0

支付宝
微信
标签:

上一篇:

下一篇:

你可能感兴趣

共有 0 - MongoDB 条件操作符和demo

博客简介

精彩评论

  • admin(6年前 (2020-03-09))

    分别用不同厚度的筏板定义,画图后这设置筏板变截面处理。 http://f.fwxgx.co...

    评:新文章!
  • admin(6年前 (2020-03-09))

    分别用不同厚度的筏板定义,画图后这设置筏板变截面处理。 http://f.fwxgx.co...

    评:新文章!
  • admin(6年前 (2020-03-09))

    新增一个框架图! http://biji.jinli.vip/wp-content/upl...

    评:新文章!
  • 一位WordPress评论者(6年前 (2020-02-13))

    嗨,这是一条评论。 要开始审核、编辑及删除评论,请访问仪表盘的“评论”页面。 评论者头像来自...

    评:世界,您好!